package com.sasis.dao.ibatis;

import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.sasis.util.ArrayPageList;
import com.sasis.util.PageRequest;
import com.sasis.util.PageList;

public class SqlMapClientWrapper {
	
	private SqlMapClientTemplate sqlMapClientTemplate;
	
	private Class entityClass;
	
	public SqlMapClientWrapper(Class entityClass, SqlMapClientTemplate sqlMapClientTemplate) {
		this.entityClass = entityClass;
		this.sqlMapClientTemplate = sqlMapClientTemplate;
	}
	
	private String getNamespaceStatement(String name) {
		return getClassName() + "." + name;
	}
	
	private String getClassName() {
		String className = entityClass.getName();
		int index = className.lastIndexOf(".");
		if (index == -1) {
			return className;
		}
		return className.substring(index + 1);
	}
	
	public Object insert(String id, Object parameterObject) {
		return sqlMapClientTemplate.insert(getNamespaceStatement(id), parameterObject);
	}

	public Object insert(String id) {
		return sqlMapClientTemplate.insert(getNamespaceStatement(id));
	}

	public int update(String id, Object parameterObject) {
		return sqlMapClientTemplate.update(getNamespaceStatement(id), parameterObject);
	}

	public int update(String id) {
		return sqlMapClientTemplate.update(getNamespaceStatement(id));
	}

	public int delete(String id, Object parameterObject) {
		return sqlMapClientTemplate.delete(getNamespaceStatement(id), parameterObject);
	}

	public int delete(String id) {
		return sqlMapClientTemplate.delete(getNamespaceStatement(id));
	}

	public Object queryForObject(String id, Object parameterObject) {
		return sqlMapClientTemplate.queryForObject(getNamespaceStatement(id), parameterObject);
	}

	public Object queryForObject(String id) {
		return sqlMapClientTemplate.queryForObject(getNamespaceStatement(id));
	}

	public List queryForList(String id, Object parameterObject) {
		return sqlMapClientTemplate.queryForList(getNamespaceStatement(id), parameterObject);
	}

	public List queryForList(String id) {
		return sqlMapClientTemplate.queryForList(getNamespaceStatement(id));
	}
	
	public PageList queryForPageList(String listId, String countId, PageRequest pageRequest) {
		return queryForPageList(listId, countId, null, pageRequest);
	}
	
	public PageList queryForPageList(String listId, String countId, Object parameterObject, PageRequest pageRequest) {
		int skip = pageRequest.getPageIndex() * pageRequest.getPageSize();
		int max = pageRequest.getPageSize();
		List list = sqlMapClientTemplate.queryForList(getNamespaceStatement(listId), parameterObject, skip, max);
		int total = ((Integer)sqlMapClientTemplate.queryForObject(getNamespaceStatement(countId), parameterObject)).intValue();
		return new ArrayPageList(list, total, pageRequest);
	}
	
	public Map queryForMap(String id, Object parameterObject, String key) {
		return sqlMapClientTemplate.queryForMap(getNamespaceStatement(id), parameterObject, key);
	}
	
	public Map queryForMap(String id, Object parameterObject, String key, String value) {
		return sqlMapClientTemplate.queryForMap(getNamespaceStatement(id), parameterObject, key, value);
	}
}
